from django.db import models
from django.contrib.auth.models import User

class PublisherManager(models.Manager):
    def get_query_set(self):
        '''
        Returns objects from last 5 days
        '''
        manager = super(PublisherManager, self)
        query = manager.get_query_set()
        return query.filter(state='published')

class Forum(models.Model):
    '''
    Nothing fancy, just an alias for forum to which you bind topics.
    '''
    name = models.CharField(maxlength=30)
    description = models.CharField(maxlength=256)
    topics  = models.IntegerField(default=0)
    date_modified = models.DateTimeField("Modified date",auto_now_add=1,editable=False)
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')),default='published')
    #so we can order forums by internal order, not just alphabetic
    orderby = models.IntegerField()
    #published objects
    active  = PublisherManager()
    objects = models.Manager()

    class Admin:
        pass
        
    def __str__(self):
        return self.name
    
class Topic(models.Model):
    '''
    Represents a topic. You don't delete a post, just mark it deleted. 
    '''
    title = models.CharField(maxlength=256)
    author = models.ForeignKey(User)
    date = models.DateTimeField("Created date",auto_now=1)
    date_modified = models.DateTimeField("Modified date",auto_now_add=1)
    content = models.TextField()
    posts = models.IntegerField(default=0)
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')),default='published')
    forum = models.ForeignKey(Forum)
    #published objects
    active  = PublisherManager()
    objects = models.Manager()

    def save(self):
        super(Topic, self).save()
        #increase the number of topics in forum
        self.forum.topics+=1
        self.forum.save()
    
    def delete(self):
        #decrease the number of topics in forum
        self.forum.topics-=1
        self.forum.save()
        super(Topic, self).delete()
        
        
    class Admin:
        pass
    
    def __str__(self):
        return self.title	

class Post(models.Model):
    '''
    Represents a post in forum. Same as topic, you don't delete posts just mark them deleted. It better for unduing :)
    '''
    author = models.ForeignKey(User)
    date = models.DateTimeField("Created date",auto_now=1)
    date_modified = models.DateTimeField("Modified date",auto_now_add=1)
    content = models.TextField("Post content")
    topic = models.ForeignKey(Topic)
    state = models.CharField(maxlength=30, choices=(('deleted','Deleted'),('published','Published'),('hidden','Hidden'),('sticky','Sticky')),default='published')
    #published objects
    active  = PublisherManager()
    objects = models.Manager()

    def save(self):
        super(Post, self).save()
        #increase the number of posts in topic
        self.topic.posts+=1
        self.topic.save()

    def delete(self):
        #decrease the number of posts in topic
        self.topic.posts-=1
        self.topic.save()
        super(Post, self).delete()
        

    
    class Admin:
        pass
    
    def __str__(self):
        return "Topic: %s - %s"%(self.topic.title,self.content)	
